<template>
  <div class="select-model-data">
    <el-dialog :visible.sync="visible" @close="closed" :modal-append-to-body="false" title="变量绑定"
               width="1000px">
      <div class="dialog-container">
        <div>
          <p>变量列表</p>
          <div class="variate-box">
            <div class="variate">
              <template v-for="i in variateList">
                <p :key="i.key" v-if="show.includes(i.key)" @click="checkedVariate(i)">{{ i.label }}</p>
              </template>
            </div>
            <div class="filed">
              <template v-if="selectType!=='data'">
                <template v-for="i in filedList">
                  <p :key="i.name" @click="bindData= i.name">
                    {{ i.name }}
                  </p>
                </template>
              </template>
              <template v-else>
                <el-tree :data="filedList" @node-click="nodeClick"></el-tree>
              </template>
            </div>
          </div>
        </div>
        <div>
          <p>绑定</p>
          <div>{{ bindData }}</div>
        </div>
      </div>
      <div slot="footer">
        <el-button style="float: left" size="small" type="danger" @click="delBind">删除绑定</el-button>

        <el-button size="small" type="primary" @click="saveBind">确定</el-button>
        <el-button size="small" @click="closed">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "select-model-data",
  props: {
    selectedWidget: Object,
    visible: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    designer: {
      type: Object,
      default: () => {
        return {}
      }
    },
    keyType: {
      type: String,
      default: () => {
        return "dataSource"
      }
    },
    show: {
      type: [String, Array],
      default: () => ["dataSource", "methods", "data"]
    }
  },
  data() {
    return {
      variateList: [
        {label: "变量", key: "data"},
        {label: "方法", key: "methods"},
        {label: "数据源", key: "dataSource"},
      ],
      filedList: [],
      bindData: "",
      selectType: "",
    }
  },
  methods: {
    nodeClick(v) {
      this.bindData = v.bind;
    },
    delBind() {
      this.selectedWidget.options[this.keyType] = this.bindData = "";
      this.closed();
    },
    saveBind() {
      this.selectedWidget.options[this.keyType] = this.bindData;
      this.closed();
    },
    checkedVariate(i) {
      this.filedList = [];
      this.selectType = i.key;
      if (i.key === "dataSource") {
        let {dataSource} = this.designer;
        if (dataSource && dataSource.length > 0) {
          this.filedList = dataSource;
        }
      }
      if (i.key === "data") {
        let {pageScript: {data}} = this.designer;
        if (data && data.length > 0) {
          let dataObj = [];
          let d = JSON.parse(data);
          let loopKey = (o, p) => {
            let arrKey = Object.keys(o)
            for (let i = 0; i < arrKey.length; i++) {
              let item = arrKey[i];
              if (p) {
                let obj = {};
                let findObj = (o_) => {
                  for (let j = 0; j < o_.length; j++) {
                    if (o_[j].label === p) {
                      obj = o_[j];
                    } else {
                      if (o_[j].children.length > 0) {
                        findObj(o_[j].children)
                      }
                    }
                  }
                }
                findObj(dataObj)

                if (JSON.stringify(obj) !== "{}") {
                  obj.children.push({
                    label: item,
                    key: item,
                    bind: obj.bind + '.' + item,
                    children: []
                  })
                }
              } else {
                dataObj.push({label: item, key: item, bind: item, children: []})
              }

              if (typeof o[item] === "object") {
                loopKey(o[item], item)
              }
            }
          }
          loopKey(d)
          this.filedList = dataObj;
        }
      }
      if (i.key === "methods") {
        let {pageScript: {methods}} = this.designer;
        if (methods && methods.length > 0) {
          methods.forEach(i => {
            this.filedList.push({name: i.funName, value: i.funBody});
          })
        }
      }
    },
    closed() {
      this.$emit("update:visible", false)
    }
  },
  mounted() {
    this.bindData = this.selectedWidget.options[this.keyType];
  }
}
</script>

<style scoped lang="scss">
.dialog-container {
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  flex-wrap: nowrap;
  height: 500px;

  > div {
    height: 100%;

    > p {
      font-size: 12px;
      font-weight: bold;
    }

    > div {
      border: 1px solid #ddd;
      border-radius: 4px;
      width: 100%;
      height: calc(100% - 60px);
    }

    .variate-box {
      display: flex;
      flex-direction: row;
      justify-content: flex-start;
      flex-wrap: nowrap;

      .variate {
        width: 45%;
        height: 100%;
        box-sizing: border-box;
        border-right: 1px solid #ddd;
        padding: 5px 0;

        > p {
          padding: 0 10px;
          cursor: pointer;

          &:hover {
            background: #f2f2f2;
          }
        }
      }

      .filed {
        width: 55%;
        height: 100%;
        padding: 5px 0;
        box-sizing: border-box;
        overflow-y: scroll;
        > p {
          padding: 0 10px;
          cursor: pointer;

          &:hover {
            background: #f2f2f2;
          }
        }
      }
    }
  }

  > div:first-child {
    width: 40%;
  }

  > div:last-child {
    width: 58%;
  }
}
</style>

<style lang="scss">
.select-model-data {
  .el-dialog {
    .el-dialog__body {
      padding: 5px 20px;
    }
  }
}
</style>
